﻿using Microsoft.AspNetCore.SignalR.Protocol;

using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.OrderTask;
using PLCS.Domain.Shared.Enums;
using PLCS.Domain.Shared.Tasks;
using PLCS.Shared.Common.Constant;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCS.App.ViewModels.Dialogs
{
    public class SendTaskDialogViewModel : HostDialogViewModel
    {
        private string _endPos, _hintMessage, _trayNo;
        private TrayType _trayType;
        private TaskType _taskType = TaskType.普通流转;
        private int _level;
        private readonly ITaskService _taskService;
        private StationDto model;

        public StationDto Model
        {
            get { return model; }
            set { SetProperty(ref model, value); }
        }

        public string EndPos
        {
            get { return _endPos; }
            set { SetProperty(ref _endPos, value); }
        }

        public string HintMessage
        {
            get { return _hintMessage; }
            set { SetProperty(ref _hintMessage, value); }
        }

        public string TrayNo
        {
            get { return _trayNo; }
            set { SetProperty(ref _trayNo, value); }
        }

        public int Level
        {
            get { return _level; }
            set { SetProperty(ref _level, value); }
        }

        public TrayType TrayType
        {
            get { return _trayType; }
            set { SetProperty(ref _trayType, value); }
        }

        public TaskType TaskType
        {
            get { return _taskType; }
            set { SetProperty(ref _taskType, value); }
        }

        public DelegateCommand SendTaskCommand { get; }

        private async void SendTask()
        {
            try
            {
                SavebuttonIsEnabled = false;
                if (string.IsNullOrEmpty(EndPos) && TaskType != TaskType.入库)
                {
                    HintMessage = "目标为不可为空";
                    return;
                }
                var task = new TaskEditDto()
                {
                    EndPos = EndPos,
                    TrayNo = TrayNo,
                    Level = Level,
                    StartPos = Model.StaNum,
                    TaskType = TaskType,
                };
                await _taskService.CreateAsync(task);
                HintMessage = "下发成功";
                Log("下发任务", task);
                Save();
            }
            catch (Exception ex)
            {
                HintMessage = ex.Message;
            }
            finally
            {
                SavebuttonIsEnabled = true;
            }
        }

        public SendTaskDialogViewModel(ITaskService taskService)
        {
            View = "下发任务弹窗";
            _taskService = taskService;
            SendTaskCommand = new DelegateCommand(SendTask);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("Value"))
            {
                Model = parameters.GetValue<StationDto>("Value");
                Title = "编辑";
            }
            else
            {
                Title = "新增";
                Model = new StationDto();
            }
        }
    }
}